package com.mexico.inloancash.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.mexico.inloancash.R;
import com.mexico.inloancash.activity.IndiaBankInfoActivity;
import com.mexico.inloancash.activity.IndiaBillActivity;
import com.mexico.inloancash.activity.IndiaCommon_SettingActivity;
import com.mexico.inloancash.activity.IndiaDiscount_Activity;
import com.mexico.inloancash.activity.IndiaCustomerServiceActivity;
import com.mexico.inloancash.activity.India_Regarding_Activity;
import com.mexico.inloancash.activity.IndiaRecommendationActivity;
import com.mexico.inloancash.activity.IndiaAnswerQuestionsActivity;
import com.mexico.inloancash.activity.Mexico_FeedBackActivity;
import com.mexico.inloancash.bean.INdia_Update_CardInfoListBean;
import com.mexico.inloancash.bean.India_User_PhoneList_Bean;
import com.mexico.inloancash.http.IndiaRequestApi;
import com.mexico.inloancash.http.IndiaDKFactory;
import com.mexico.inloancash.utils.IntentUtils;
import com.mexico.inloancash.utils.MyPreferencesManager;
import com.mexico.inloancash.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class IndiaPersonalFragment extends Fragment {

    @BindView(R.id.head_img)
    ImageView mHeadImg;

    @BindView(R.id.phone)
    TextView mPhone;


    @BindView(R.id.about_linear)
    LinearLayout mIndia_About_Linear;


    private View minePage;
    Unbinder unbinder;


    private String mLinkIns = "https://www.instagram.com/";

    private String mLinkFacebook = "https://www.facebook.com/";

    @BindView(R.id.update_bank_linear)
    LinearLayout mIndiaUpdateBankLinear;

    private String orderno="";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        minePage = inflater.inflate(R.layout.india_activity_mine_view_page_layout_fragment, container, false);
        unbinder = ButterKnife.bind(this, minePage);
        EventBus.getDefault().register(this);//注册一个eventBus
        jump_Get_Service();
        return minePage;
    }

    @SuppressLint("CheckResult")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(INdia_Update_CardInfoListBean event) {

        //如果登录哪里 是走接口获取的短信  那么这里要放开注释 需求  随机验证码方式 这里要注释

     /*   if (event.getCode() == 200) {
            mIndiaUpdateBankLinear.setVisibility(View.VISIBLE);
            orderno=event.getOrderno();
        }else if(event.getCode() == 500){
            mIndiaUpdateBankLinear.setVisibility(View.GONE);
            orderno="";
        }*/
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPhone.setText(Utils.hidePhone(MyPreferencesManager.getString("myPhone", "")));
        //Log.e("data",MyPreferencesManager.getString("myPhone", "").toString());
    }

    @OnClick({ R.id.about_linear,
            R.id.update_bank_linear,R.id.tv_bill})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.loan_linear:
//                Intent bill = new Intent(getContext(), IndiaBillActivity.class); //借款订单
//                startActivity(bill);
//                break;
//            case R.id.bank_info_linear:
//                Intent back = new Intent(getContext(), IndiaDiscount_Activity.class);//银行卡
//                startActivity(back);
////                Intent bank = new Intent(getContext(), BankActivity.class);//银行卡
////                startActivity(bank);
//                break;
//            case R.id.question_linear:
//                Intent question = new Intent(getContext(), IndiaAnswerQuestionsActivity.class);//常见问题
//                startActivity(question);
//                break;
            case R.id.about_linear:
                Intent about = new Intent(getContext(), Mexico_FeedBackActivity.class);//关于我们
                startActivity(about);
                break;
//            case R.id.connect_linear:
//                //打电话
//                Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + MyPreferencesManager.getString("phone", "")));//跳转到拨号界面，同时传递电话号码
//                startActivity(dialIntent);
//                break;
//            case R.id.set_linear:
//                Intent setting = new Intent(getContext(), IndiaCommon_SettingActivity.class);//设置
//                startActivity(setting);
//                break;
            case R.id.layout_set:
                Intent auth = new Intent(getContext(), IndiaBankInfoActivity.class);
                auth.putExtra("orderno",orderno);
                startActivity(auth);
                //跳转到修改银行卡界面
                break;
            case R.id.tv_bill:
                Intent bill = new Intent(getContext(), IndiaBillActivity.class); //借款订单
                startActivity(bill);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @OnClick(R.id.layout_set)
    public void jump_Settings() {
        Intent setting = new Intent(getContext(), IndiaCommon_SettingActivity.class);//设置
        startActivity(setting);
    }

//    @OnClick(R.id.layoutIns)
//    public void jump_OnClickIns() {
//        IntentUtils.openWeb(getActivity(), mLinkIns);
//    }
//
//    @OnClick(R.id.layoutFacebook)
//    public void jump_OnClick_Facebook() {
//        IntentUtils.openWeb(getActivity(), mLinkFacebook);
//    }
//
//    @OnClick(R.id.layoutCustomerService)
//    public void jump_Customer_Service() {
//        Intent setting = new Intent(getContext(), IndiaCustomerServiceActivity.class);
//        startActivity(setting);
//    }
//
//    @OnClick(R.id.layoutFeedback)
//    public void jump_Feedback() {
//        Intent setting = new Intent(getContext(), IndiaRecommendationActivity.class);
//        startActivity(setting);
//    }

    public void jump_Get_Service() {
        Log.d("loan", "getService");
        IndiaRequestApi api = IndiaDKFactory.getInstance().getApi();
        Call<String> getService = api.getPhone();
        getService.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d("loan", "getService onResponse:" + response.body());
                if (response.isSuccessful()) {
                    India_User_PhoneList_Bean bean = new Gson().fromJson(response.body(), India_User_PhoneList_Bean.class);
                    if (bean.getCode() == 200) {
                        mLinkIns = mLinkIns + bean.getData().getIns();
                        mLinkFacebook = mLinkFacebook + bean.getData().getIns();
//                        mIndia_TvIns.setText(bean.getData().getIns());
//                        mIndia_Tv_Facebook.setText(bean.getData().getFacebook());
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
            }
        });
    }
}
